package com.jshcbd.mvp_kt_demo.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import com.jshcbd.mvp_kt_demo.R
import com.jshcbd.mvp_kt_demo.view.LVCircularRing

class LoadingDialog(context: Context?, msg: String?) {
    var mLoadingView: LVCircularRing
    var mLoadingDialog: Dialog?
    fun show() {
        if (mLoadingDialog != null && !mLoadingDialog!!.isShowing) {
            mLoadingDialog!!.show()
            mLoadingView.startAnim()
        }
    }

    fun close() {
        if (mLoadingDialog != null && mLoadingDialog!!.isShowing) {
            mLoadingView.stopAnim()
            mLoadingDialog!!.dismiss()
        }
    }

    val isShowing: Boolean
        get() {
            if (mLoadingDialog != null) {
                return mLoadingDialog!!.isShowing
            }
            return false
        }

    init {
        // 首先得到整个View
        val view = LayoutInflater.from(context).inflate(R.layout.layout_dialog_loading, null)
        // 获取整个布局
        val layout = view.findViewById<LinearLayout>(R.id.dialog_view)
        // 页面中的LoadingView
        mLoadingView = view.findViewById(R.id.lv_circular_ring)
        // 页面中显示文本
        val loadingText = view.findViewById<TextView>(R.id.loading_text)
        // 显示文本
        loadingText.text = msg
        // 创建自定义样式的Dialog
        mLoadingDialog = Dialog(context!!, R.style.loading_dialog)
        // 设置返回键无效--true有效 false无效
        mLoadingDialog!!.setCancelable(false)
        mLoadingDialog!!.setContentView(layout, LinearLayout.LayoutParams(300, 300))
    }
}